<?php
/**
 * User not an administrator, redirect to Home page
 * automatically.
 */
if(!$session->isAdmin()){
   header("Location: ../index.php");
}
else{ ?>
<div id="form_container" style="border:none; width:800px; margin: 10px 78px 0px 78px;">
	<div id="form_text" style="margin:0px;" class="settings">
    	<h2>Banned Users List</h2>
    </div>
    <div id="form_content" class="settings" style="text-align:center">
        <div class="tableFilter">
            <form id="tableFilter" action="" method="post" onsubmit="myTable.filter(this.id); return false;"><b>Filter: </b> 
                <select id="column" class="select">
                    <option value="1">Firstname</option>
                    <option value="2">Lastname</option>
                    <option value="3">Email</option>
                </select>
                <input type="text" id="keyword" />
                <input class="submit" type="submit" value="Search" />
                <input class="submit" type="reset" value="Clear" />
            </form>
        </div>
        
        <!-- The navigation buttons between pages -->
        <?php include('../include/pagination.php');
		if (isset($_POST['orderby'])){
			$orderby=$_POST['orderby'];
		} else {
			$orderby = 'orgName';
		}
		$result = $pagination->paginatePage("25","organizations","$orderby"); ?>
        
        <!-- The table of users -->
        <table id="myTable" cellpadding="0" cellpadding="0">
            <thead>
            	<th axis="number">ID</th>
                <th axis="string">Organization</th>
                <th axis="string">Category</th>
                <th></th>
            </thead>
            <tbody>
            	<?php while ($row = $result->fetch()) {
					$orgID = $row['orgID'];
					$orgName = $row['orgName'];
					$category = $row['category'];
					switch($category)
					{
						case "other":
							$category = "Other";
							break;
						case "farm":
							$category = "Farm";
							break;
						case "farmers_market":
							$category = "Farmer's Market";
							break;
						case "community_garden":
							$category = "Community Garden";
							break;
						case "orchard":
							$category = "Orchard";
							break;
						case "csa":
							$category = "CSA";
							break;
					}		
					echo "<tr><td>".$orgID."</td>
					<td><a href='orgInfo.php?id=$orgID'>".$orgName."</a></td>
					<td>".$category."</td>
					<td><a href='editOrg.php?id=$orgID'>Edit</a></td></tr>";
				}?>
            </tbody>
            <tfoot>
                <tr style="visibility:hidden">
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
            </tfoot>
        </table>
        </div>
	</div>
</div>
<script type="text/javascript">
	var myTable = {};
	window.addEvent('domready', function(){
		myTable = new sortableTable('myTable', {overCls: 'over', onClick: function(){}});
	});
</script>
<?php } ?>

<?php
/**
 * User not an administrator, redirect to Home page
 * automatically.
 *
if(!$session->isAdmin()){
   header("Location: ../index.php");
}
else{
	/**
	 * Administrator is viewing page, so display all options/forms
	 *
	echo '<div id="content">
			<div id="title">
			<h2>Organizations List</h2>
			<p>List of organization organized by the organization name</p>
			</div>
			<p align="center"><a href="?all">All</a> | <a href="?by=A">A</a> | <a href="?by=B">B</a> | <a href="?by=C">C</a> | <a href="?by=D">D</a> | <a href="?by=E">E</a> | <a href="?by=F">F</a> | <a href="?by=G">G</a> | <a href="?by=H">H</a> | <a href="?by=I">I</a> | <a href="?by=J">J</a> | <a href="?by=K">K</a> | <a href="?by=L">L</a> | <a href="?by=M">M</a> | <a href="?by=N">N</a> | <a href="?by=O">O</a> | <a href="?by=P">P</a> | <a href="?by=Q">Q</a> | <a href="?by=R">R</a> | <a href="?by=S">S</a> | <a href="?by=T">T</a> | <a href="?by=U">U</a> | <a href="?by=V">V</a> | <a href="?by=W">W</a> | <a href="?by=X">X</a> | <a href="?by=Y">Y</a> | <a href="?by=Z">Z</a></p>';
	if(isset($_GET['by']) && !isset($_GET['all'])){
		sortByLetter();
	}
	else {
		/* Display User Table *
		displayOrg();
	}
}

/**
 *displayOrganization - Displays the organization table in a nicely formatted html table
 **
function displayOrg(){
	// number of results to show per page
	$per_page = 25;		
	// figure out the total pages in the database
	$result = mysql_query("SELECT * FROM organizations ORDER BY orgName ASC");
	$total_results = mysql_num_rows($result);
	$total_pages = ceil($total_results / $per_page);
	// Error checking result
	if(!$result || ($total_results < 0)){
		echo "<p>Error displaying info</p>
			</div>";
		return;
	}
	if($total_results == 0){
		echo "<p>There are no result.</p>
			</div>";
		return;
	}
	else {
		// check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
		if (isset($_GET['page']) && is_numeric($_GET['page'])){
			$show_page = $_GET['page'];
						
			// make sure the $show_page value is valid
			if ($show_page > 0 && $show_page <= $total_pages){
				$start = ($show_page -1) * $per_page;
				$end = $start + $per_page; 
			}
			else{
				// error - show first set of results
				$start = 0;
				$end = $per_page; 
			}               
		}
		else{
			// if page isn't set, show first set of results
			$start = 0;
			$end = $per_page; 
		}	
		// display pagination
		echo "<p><b>View Page:</b> ";
		for ($i = 1; $i <= $total_pages; $i++){
			echo "<a href='?page=$i'>$i</a> ";
		}
		echo "</p>";			  
		// display data in table
		echo "<table border='1' cellpadding='2' cellspacing='0'>";
		echo "<tr> <th width='428px'>Organization Name</th> <th width='150px'>Category</th> <th></th> <th></th> </tr>";
		// loop through results of database query, displaying them in the table 
		for ($i = $start; $i < $end; $i++){
			// make sure that PHP doesn't try to show results that don't exist
			if ($i == $total_results) { break; }	
			// echo out the contents of each row into a table
			$orgName = mysql_result($result, $i, 'orgName');
			$category = mysql_result($result, $i, 'category');
			$orgID = mysql_result($result, $i, 'orgID');
			switch($category)
			{
				case "other":
					$category = "Other";
					break;
				case "farm":
					$category = "Farm";
					break;
				case "farmers_market":
					$category = "Farmer's Market";
					break;
				case "community_garden":
					$category = "Community Garden";
					break;
				case "orchard":
					$category = "Orchard";
					break;
				case "csa":
					$category = "CSA";
					break;
			}
			echo "<tr>
					<td><a href='orgInfo.php?id=$orgID'>$orgName</a></td>
					<td>$category</td>
					<td><a href='editOrg.php?id=$orgID'>Edit</a></td>
					<td><a href='deleteOrg.php?id=$orgID'>Delete</a></td>
				 </tr>"; 
		}
		// close table>
		echo "</table></div>";
		// pagination 
	}
}
//Sort by letter
function sortByLetter(){
	$letter=$_GET['by'];
	//connect to database
	$result = mysql_query("SELECT * FROM organizations WHERE orgName LIKE '$letter%' ORDER BY orgName ASC");
	$total_results = mysql_num_rows($result);
	// Error checking result
	if(!$result || ($total_results < 0)){
		echo "<p>Error displaying info</p>
			</div>";
		include('footer.php');
		return;
	}
	else if($total_results == 0){
		echo "<p>There are no result for $letter</h4>.</p>
			</div>";
		include('footer.php');
		return;
	}
	else {
		
		$start = 0;
		$end = $total_results; 
					  
		// display data in table
		echo "<p><b>Sort by: </b>$letter</p>
		<div style='overflow:auto; height:84%'>
		<table border='1' cellpadding='2' cellspacing='0'>";
		echo "<tr> <th width='428px'>Organization Name</th> <th width='150px'>Category</th> <th></th> <th></th></tr>";
		// loop through results of database query, displaying them in the table 
		for ($i = $start; $i < $end; $i++){
			// make sure that PHP doesn't try to show results that don't exist
			if ($i == $total_results) { break; }	
			// echo out the contents of each row into a table
			$orgName = mysql_result($result, $i, 'orgName');
			$category = mysql_result($result, $i, 'category');
			$orgID = mysql_result($result, $i, 'orgID');
			switch($category)
			{
				case "other":
					$category = "Other";
					break;
				case "farm":
					$category = "Farm";
					break;
				case "farmers_market":
					$category = "Farmer's Market";
					break;
				case "community_garden":
					$category = "Community Garden";
					break;
				case "orchards":
					$category = "Orchards";
					break;
				case "csa":
					$category = "CSA";
					break;
			}
			
			echo "<tr>
					<td><a href='orgInfo.php?id=$orgID'>$orgName</a></td>
					<td>$category</td>
					<td><a href='editOrg.php?id=$orgID'>Edit</a></td>
					<td><a href='deleteOrg.php?id=$orgID'>Delete</a></td>
				 </tr>"; 
		}
		// close table>
		echo "</table></div></div>";
		// pagination 
	}
}*/?>